package dao;

import domain.Car;
import domain.Goods;
import util.DbUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsDao {
    DbUtil dbu = new DbUtil();
    public int pageSize=5;

//    通过商品名称遍历商品的所有信息
    public List<Goods> findUserByName(String goods){
        String sql= "select * from goods where goods_name like '%"+goods+"%'";
        System.out.println(sql);
        ResultSet rs = dbu.executeQuery(sql);
        List<Goods> good = new ArrayList<Goods>();
        try {
            while (rs.next()){
                Goods g = converToGood(rs);
                good.add(g);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return good;
    }

    public List<Car> findCarsByUserid(int userid){
        String sql= "select * from car where user_id="+userid;
//        String sql= "select * from car where user_id="+896733;
        System.out.println(sql);
        ResultSet rs = dbu.executeQuery(sql);
        List<Car> cars = new ArrayList<Car>();
        try {
            while (rs.next()){
                Car car= converToCar(rs);
                cars.add(car);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

        return cars;
    }


//    通过商品ID获得商品的信息
    public Goods findGoodsById(int id){
        String sql= "select * from goods where goods_id="+id;
        ResultSet rs = dbu.executeQuery(sql);
        try {
            if(rs.next()){
                return converToGood(rs);
            } else {
                return null;
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }
    }

    //获取购物车商品的数量信息
    public String findnum(String goodsname,String userid){
        String sql = "select num from car where goods_pic='"+goodsname+"' and user_id='"+userid+"'";
        System.out.println(sql);
        ResultSet rs = dbu.executeQuery(sql);
        try {
            if (rs.next()){
                return rs.getString("num");
            }else {
                return null;
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }

    }

    public Car converToCar(ResultSet rs)  {
        Car car = null;
        try {
            int id = rs.getInt(1);
            String userid = rs.getString(2);
            String goodspic = rs.getString(3);
            String goodsprice = rs.getString(4);
            int num = rs.getInt(5);
            car = new Car(id, userid, goodspic, goodsprice, num);
            return car;
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }
    }


    public Goods converToGood(ResultSet rs){
        Goods g = null;
        try {
            int goodsid = rs.getInt(1);
            String goodsname = rs.getString(2);
            int type = rs.getInt(3);
            String goodsprice = rs.getString(4);
            String goodsdesc = rs.getString(5);
            String supplier = rs.getString(6);
            int qty = rs.getInt(7);
            String goodsimage = rs.getString(8);
            g = new Goods(goodsid,goodsname,type,goodsprice,goodsdesc,supplier,qty,goodsimage);
            return g;
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }
    }

    public int getTotal(int user_id){
//        String sql= "select count(1) as total from car where user_id = "+896733+" ";
        String sql= "select count(1) as total from car where user_id = "+user_id+" ";
        System.out.println(sql);
        ResultSet rs = dbu.executeQuery(sql);
        int total = 0;
        try {
            if (rs.next()) {
                total=rs.getInt("total");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return total;
    }


}
